﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;

namespace InstantSpine.GUI
{
    static class ImagingTools
    {
        /// <summary>
        /// Converts a GDI+ bitmap to a WPF compatible BitmapSource object
        /// </summary>
        /// <param name="source">GDI+ bitmap (System.Drawing.Bitmap object)</param>
        /// <returns>BitmapSource for use in WPF apps</returns>
        public static BitmapSource GdiToWpfBitmap(this Bitmap source) {
            IntPtr ip = source.GetHbitmap();
            BitmapSource bs = null;
            try {
                bs = Imaging.CreateBitmapSourceFromHBitmap(
                    ip,
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());
            }
            finally {
                NativeMethods.DeleteObject(ip);
            }
            return bs;
        }
    }
}
